﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Command;

using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools.Extension;

using Stock.Model;
using System.Windows.Controls;
using System.Windows.Input;
using Stock.Service;
using Stock.Util;

namespace Stock.ViewModel
{
    public class AddStockViewModel : ViewModelBase, IDialogResultable<StockViewModel>
    {
        private EasyMoneyService service;
        public AddStockViewModel()
        {
            result = new StockViewModel();
            stockList = new ObservableCollection<StockViewModel>();
            service = SimpleIoc.Default.GetInstance<EasyMoneyService>();
        }

        private StockViewModel result;
        public StockViewModel Result { get => result; set => Set(ref result, value); }
        public Action CloseAction { get; set; }

        public RelayCommand AddCmd => new RelayCommand(Add);

        public RelayCommand CancelCmd => new RelayCommand(Cancel);

        private ObservableCollection<StockViewModel> stockList;

        public ObservableCollection<StockViewModel> StockList { get => stockList; set => stockList = value; }

        public RelayCommand<KeyEventArgs> KeyUpCmd => new RelayCommand<KeyEventArgs>(KeyUp);

        public RelayCommand<SelectionChangedEventArgs> SelectionChangeCmd => new RelayCommand<SelectionChangedEventArgs>(SelectionChange);

        private void SelectionChange(SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                Result = e.AddedItems[0] as StockViewModel;
            }
        }

        private void KeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                System.Windows.Controls.TextBox textBox = e.OriginalSource as System.Windows.Controls.TextBox;
                string word = textBox?.Text;
                if (string.IsNullOrEmpty(word))
                {
                    return;
                }

                Clear();
                List<StockViewModel> list = AppUtil.ToViewModelList(service.GetStockList(word), true);
                if (list != null && list.Count > 0)
                {
                    foreach (StockViewModel item in list)
                    {
                        stockList.Add(item);
                    }

                    if (e.Source is HandyControl.Controls.ComboBox box)
                    {
                        box.IsDropDownOpen = true;
                        textBox.TabIndex = -1;
                    }
                }
            }
        }

        public void Clear()
        {
            stockList.Clear();
            result = new StockViewModel();
        }

        private void Cancel()
        {
            result = null;
            CloseAction?.Invoke();
        }

        private void Add()
        {
            if (!result.Valid())
            {
                Growl.Error(new GrowlInfo()
                {
                    Message = AppConst.TEXT_ADD_STOCK_ERROR,
                    ShowCloseButton = true,
                    ShowDateTime = false,
                    StaysOpen = false,
                    WaitTime = 4,
                    IsCustom = true,
                    Token = AppConst.TOKEN_ADD_STOCK
                });
                return;
            }
            if (result.Number > 0)
            {
                result.Number = StringUtil.ToFixed(result.Number);
            }
            CloseAction?.Invoke();
        }
    }
}
